Skip to content

Optionally disable hugepages for stacks #4001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stedolan
Copy link
Contributor

@stedolan stedolan commented May 14, 2025

Hugepages are not necessarily a good idea for stacks: they mean that every stack takes 2MB of RAM, even though most programs confine themselves to a few k of stacks. This patch adds a new GC tweak to optionally disable hugepages for stacks (Xnohugepage_stacks=1), by passing MADV_NOHUGEPAGE to madvise.

On this program:

let n = Atomic.make 0
let () =
  let _ = List.init 100 (fun _ -> Thread.create (fun () -> Atomic.incr n; Unix.sleep 100) ()) in
  while Atomic.get n < 100 do Thread.yield () done;
  ()

the new flag makes the memory consumption go from 400 MB to 200 MB. (I think the remaining 2MB per thread is the glibc pthread stack).

@NickBarnes
Copy link
Contributor

I concur that the remaining 2MiB Rss per thread is glibc pthread stacks (which are incidentally mapped at 9.56 MiB); we could trim these if we cared, by mapping them ourselves and using pthread_attr_setstack. Each thread also gets a 132k mapping (with 8k of RSS) from the GlibC malloc arena, which we don't care about.

Copy link
Contributor

@NickBarnes NickBarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Manually tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants